package com.myk.game.gobangchess.rooms.absrooms;

import com.myk.game.gobangchess.*;
import com.myk.game.gobangchess.constants.Msger;

public abstract class AbsMultiViewRoom extends Room {

    public AbsMultiViewRoom(String roomName, String roomIntroduce, String chessName, int[][] curGrids, boolean isSupportPersonal, boolean isSaveHistoryToFile,String ... modes) {
        super(roomName, roomIntroduce, chessName, curGrids, isSupportPersonal, isSaveHistoryToFile,modes);
    }

    /**
     * 返回可能需要旋转的index
     *
     * @param row
     * @param col
     * @param rotate
     * @return
     */
    public int getIndex(int row, int col, boolean rotate) {
        if (rotate) {
            return getRotatedIndex(getIndex(row, col));
        }
        return getIndex(row, col);
    }

    /**
     * 返回旋转后的位置
     */
    public int[] getRotatedPos(int row, int col) {
        return new int[]{curGrids.length - row - 1, curGrids[0].length - col - 1};
    }

    /**
     * 返回旋转后的index
     */
    public int getRotatedIndex(int index) {
        return curGrids.length * curGrids[0].length - index - 1;
    }

    /**
     * 返回带视角的game info
     * view: 0->观众; 1->黑; 2->白; 3->同时黑和白，自己和自己下
     */
    public String getGameInfo(int view) {
        return super.getGameInfo();
    }

    public String getCoordMode(int view) {
        if (view == 2) return "22220000";
        else return "11110000";
    }

    /**
     * 下棋方法,先判断下棋人，然后交给落子规则
     */
    public void moveLater(Player player, int row, int col) {
        int view = 0;
        if (player_black == player_white) {
            view = 3;
            if (cur == 3) {
                doRule(row, col, 1, 3);
                doRule(row, col, 2, 3);
                return;
            }
        }
        if (player_black == player) {
            if ((cur & 1) > 0) {
                doRule(row, col, 1, view | 1);
            }
        }
        if (player_white == player) {
            if ((cur & 2) > 0) {
                doRule(row, col, 2, view | 2);
            }
        }
    }

    /**
     * 带着view的doRule，因为视角可能会影响落子判断
     *
     * @param row
     * @param col
     * @param value
     * @param view
     */
    public void doRule(int row, int col, int value, int view) {
        doRule(row, col, value);
    }

    public void doRule(int row, int col, int value) {
    }

    /**
     * 玩家进入房间默认是观众
     */
    public void playerEnter(Player player) {
        removeSpectator(player);//先移出，避免因奇怪原因玩家多次进入，出现多个观众的问题
        addSpectator(player);
        player.setEnteringRoom(this);

        player.sendMessage(Msger.enterRoomSuccess, "Ok");
        player.sendMessage(Msger.refreshGameInfo, getGameInfo(0)); //刚进来更新一下游戏数据
        noticeAllRefreshRoomInfo();
        DataManager.noticeAllRefreshRoomList();
        noticePlayerCurrentCountDown(player); //通知刚进来的用户显示当前的倒计时动画
        System.out.println(player.getNickNameOrTemp() + "进入" + getRoomName());
    }

    /**
     * 用户重连方式进入房间
     */
    public void playerReConnectEnter(Player player) {
        player.sendMessage(Msger.enterRoomSuccess, "Ok"); //通知你进入房间成功
        player.sendMessage(Msger.refreshPlayerInfo, player.getPlayerInfo()); //更新角色
        player.sendMessage(Msger.refreshRoomInfo, getRoomInfo()); //更新一下游戏数据
        System.out.println(player.getNickNameOrTemp() + "重连到" + getRoomName());
        boolean isPlayingStarted = player_black != null && player_white != null; //黑白位置都有玩家，说明对弈是开始的状态
        boolean isOnSeatPlayer = player == player_black || player == player_white; //重连的玩家属于在座玩家
        if (isPlayingStarted && isOnSeatPlayer) { //checkGameStart()中满足要求了会刷新界面，用这种方式防止重复刷新
            if (overFlag == 2 || overFlag == 3 || overFlag == 4) { //对弈结束状态成为棋手时，分发继续按钮
                overFlag = 2; //重进双方重新确认判断
                dispatchContinueButton();
            }
            noticeOnSeatReConnectMove(player);
        }
        else {
            if (player_black == player){
                String blackGameInfo = getGameInfo(1);
                player.sendMessage(Msger.refreshGameInfo, blackGameInfo);
            }
            else if (player_white == player){
                String whiteGameInfo = getGameInfo(2);
                player.sendMessage(Msger.refreshGameInfo, whiteGameInfo);
            }
            else{
                String specGameInfo = getGameInfo(0);
                player.sendMessage(Msger.refreshGameInfo, specGameInfo);
            }
        }
    }

    @Override
    public void noticeOnSeatReConnectMove(Player player) {
        if (cur == 1) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else if (cur == 2) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else if (cur == 3) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else {
            player.sendMessage(Msger.youCanMove, "Ok");
        }

        if (player_black == player_white && player_black == player){
            String compGameInfo = getGameInfo(3);
            player.sendMessage(Msger.refreshGameInfo, compGameInfo);
        }
        else if (player_black == player) {
            String blackGameInfo = getGameInfo(1);
            player.sendMessage(Msger.refreshGameInfo, blackGameInfo);
        }
        else if (player_white == player) {
            String whiteGameInfo = getGameInfo(2);
            player.sendMessage(Msger.refreshGameInfo, whiteGameInfo);
        }
        else {
            String specGameInfo = getGameInfo(0);
            player.sendMessage(Msger.refreshGameInfo, specGameInfo);
        }

        player.dispatchCustomBottom();
        noticePlayerCurrentCountDown(player);
    }

    //通知房间里所有人更新游戏信息
    protected void noticeAllRefreshGameInfo() {
        if (player_black != null && player_white != null && player_black == player_white) {
            player_black.sendMessage(Msger.refreshGameInfo, getGameInfo(3)); //个人模式就只显示一次
        }
        else {
            if (player_black != null) {
                player_black.sendMessage(Msger.refreshGameInfo, getGameInfo(1));
            }
            if (player_white != null) {
                player_white.sendMessage(Msger.refreshGameInfo, getGameInfo(2));
            }
        }
        for (Player player : spectatorList) {
            if (player != null) {
                player.sendMessage(Msger.refreshGameInfo, getGameInfo(0));
            }
        }
    }
}
